#include "engraphicsview.h"
#include <QResizeEvent>
#include <QDebug>
#include <enscene.h>
#include <enellipseitem.h>

EnGraphicsView::EnGraphicsView( QWidget* parent )
    : QGraphicsView( parent )
{
    init();
}

EnGraphicsView::EnGraphicsView()
{
    init();
}

EnGraphicsView::~EnGraphicsView()
{
    delete selectionRect;
}

void EnGraphicsView::init()
{
    setWindowTitle( "Engine Main Window" );
    setRenderHints( QPainter::Antialiasing | QPainter::SmoothPixmapTransform );

    QGLFormat glFormat( QGL::SampleBuffers );
    //glFormat.setSwapInterval( 1 ); // set VSync
    setViewport( new QGLWidget( glFormat ) ); // Set Anti-Aliasing and OpenGL
    setViewportUpdateMode( QGraphicsView::FullViewportUpdate );
    setCacheMode( QGraphicsView::CacheBackground );

    // set scene wait time by calculating fps to milliseconds
    setScene( new EnScene() );
    show();

    selectionRect = new QGraphicsRectItem( QRect( 0, 0, 0, 0 ) );
    selectionRect->setPen( QColor( 0, 0, 0, 200 ) );
    selectionRect->setBrush( QColor( 255, 255, 255, 30 ) );
    selectionRect->setZValue( 10000 );
    scene()->addItem( selectionRect );
    selectionRect->hide();
}

void EnGraphicsView::resizeEvent( QResizeEvent *event )
{
    if( scene() )
        scene()->setSceneRect( QRect( QPoint( 0, 0 ), event->size() ));
    QGraphicsView::resizeEvent( event );
}

// Here we delete items
void EnGraphicsView::keyPressEvent( QKeyEvent *event )
{
    //if( event->text() == "x" && event->matches( QStandardKey key
    if( event->matches( QKeySequence::Cut ) || event->matches( QKeySequence::Delete ) ) {

        QList<QGraphicsItem*> items = this->scene()->selectedItems();
        EnEllipseItem* p;
        for( int i = 0; i < items.size(); i++ )
        {
            p = qgraphicsitem_cast<EnEllipseItem*>( items.at( i ) );
            module->localRemoveBody( this->scene(), p->parentBody->getMapKey() );
        }

    }
    // Always remember to call the parent's method
    QGraphicsView::keyPressEvent( event );
}

void EnGraphicsView::mouseMoveEvent( QMouseEvent *event )
{
    if( event->buttons().testFlag( Qt::LeftButton ) )
    {
        // handle selectionRect
        if( selectionRect->isVisible() )
        {
            QPoint mouseDownView = mapFromScene( mouseDownPos );
            lastMouseViewPos = event->pos();
            QRect rubberRect( mouseDownView, lastMouseViewPos );
            rubberRect = rubberRect.normalized();
            selectionRect->setRect( rubberRect );
            QPolygonF p = mapToScene( rubberRect );
            QPainterPath path;
            path.addPolygon( p );
            scene()->setSelectionArea( path, Qt::IntersectsItemShape );
        }
    }

    // Always remember to have the parent class handle the event
    QGraphicsView::mouseMoveEvent( event );
}

void EnGraphicsView::mouseReleaseEvent( QMouseEvent *event )
{
    selectionRect->hide();
    // Always remember to have the parent class handle the event
    QGraphicsView::mouseReleaseEvent( event );
}

void EnGraphicsView::mousePressEvent( QMouseEvent *event )
{
    // Always remember to have the parent class handle the event
    QGraphicsView::mousePressEvent( event );

    if( !scene()->itemAt( mapToScene( event->pos() ) ) )
    {
        mouseDownPos = QGraphicsView::mapToScene( event->pos() ).toPoint();
        lastMouseViewPos = event->pos();
        selectionRect->setRect( mapFromScene( mouseDownPos ).x(), mapFromScene( mouseDownPos ).y(), 0, 0 );
        selectionRect->show();
    }
}

void EnGraphicsView::setModule( EnModule* m )
{
    this->module = m;
}
